home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / board / Chaos53src.lha / chaos / src / ProjectAmi.c < prev    next >
C/C++ Source or Header  |  1994-11-19  |  13KB  |  495 lines

  1. /*  Chaos:            The Chess HAppening Organisation System    V5.3
  2.     Copyright (C)   1993    Jochen Wiedmann
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19.     $RCSfile: ProjectAmi.c,v $
  20.     $Revision: 3.4 $
  21.     $Date: 1994/11/19 19:32:01 $
  22.  
  23.     This file contains the system dependent functions that support the
  24.     Project menu.
  25.  
  26.     Computer:    Amiga 1200            Compiler:    Dice 2.07.54 (3.0)
  27.  
  28.     Author:    Jochen Wiedmann
  29.         Am Eisteich 9
  30.       72555 Metzingen
  31.         Tel. 07123 / 14881
  32.         Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
  33. */
  34.  
  35.  
  36. #ifndef CHAOS_H
  37. #include "chaos.h"
  38. #endif
  39.  
  40. #ifdef AMIGA
  41. #ifndef LIBRARIES_ASL_H
  42. #include <libraries/asl.h>
  43. #endif
  44. #ifndef LIBRARIES_GADTOOLS_H
  45. #include <libraries/gadtools.h>
  46. #endif
  47. #ifndef DOS_DOS_H
  48. #include <libraries/dos.h>
  49. #endif
  50. #ifndef CLIB_ASL_PROTOS_H
  51. #include <clib/asl_protos.h>
  52. #endif
  53. #ifndef CLIB_ICON_PROTOS_H
  54. #include <clib/icon_protos.h>
  55. #endif
  56.  
  57. #ifdef AZTEC_C
  58. #ifndef PRAGMAS_ASL_LIB_H
  59. #include <pragmas/asl_lib.h>
  60. #endif
  61. #ifndef PRAGMAS_ICON_LIB_H
  62. #include <pragmas/icon_lib.h>
  63. #endif
  64. #define strrchr rindex
  65. #endif    /*  AZTEC_C */
  66.  
  67. #if defined(_DCC)  ||  defined(__SASC)  ||  defined(__MAXON__)
  68. #ifndef PRAGMAS_ASL_PRAGMAS_H
  69. #include <pragmas/asl_pragmas.h>
  70. #endif
  71. #ifndef PRAGMAS_ICON_PRAGMAS_H
  72. #include <pragmas/icon_pragmas.h>
  73. #endif
  74. #endif    /*  _DCC  ||  __SASC  ||  __MAXON__ */
  75.  
  76. #ifdef __GNUC__
  77. #ifndef _INLINE_ASL_H
  78. #include <inline/asl.h>
  79. #endif
  80. #ifndef _INLINE_ICON_H
  81. #include <inline/icon.h>
  82. #endif
  83. #endif    /*  __GNUC__    */
  84.  
  85. #endif    /*  AMIGA   */
  86.  
  87.  
  88.  
  89.  
  90. /*
  91.     FileRequest() creates a file requester which reads a file name.
  92.  
  93.     Inputs: defaultfile a pointer to a string containing the default name
  94.         title    a pointer to a string containing the requester's
  95.             title. This may be NULL, in which case
  96.             MSG_CDAT_SELECTION is assumed.
  97.         ending    a pointer to a string containing the default ending.
  98.             This may be NULL, in which case "#?.cdat" is assumed.
  99.             Note, that this MUST be something like "#?.xxx" on
  100.             the Amiga!
  101.         savemode    TRUE, if non-existing files may be selected.
  102.  
  103.     Result: Full path name of the file, that was selected or NULL, if the
  104.         user cancelled.
  105. */
  106. char *FileRequest(char *defaultfile, char *title, char *ending, int savemode)
  107.  
  108. #ifdef AMIGA
  109. { struct FileRequester *requester;
  110.   char pattern[20];
  111.   char *result = NULL;
  112.   char *filename, *pathname, *endptr;
  113.   BPTR dir;
  114.   static char FileRequestName[TRNFILENAME_LEN+1];
  115.   static char PathName[TRNFILENAME_LEN+1];
  116.   struct Window *window;
  117.  
  118.  
  119.   /*
  120.       Bring up default settings, if needed.
  121.   */
  122.   if (title == NULL)
  123.   { title = (char *) GetChaosString(MSG_CDAT_SELECTION);
  124.   }
  125.   if (ending == NULL)
  126.   { ending = "#?.cdat";
  127.   }
  128.  
  129.  
  130.   /*
  131.       Get Intuition window pointer from MUI window, allocate Filerequester
  132.       and parse the ending for wildcards.
  133.   */
  134.   get(MainWnd, MUIA_Window_Window, &window);
  135.   if ((requester = (struct FileRequester *)
  136.            MUI_AllocAslRequest(ASL_FileRequest, NULL))  ==  NULL)
  137.   { MemError();
  138.     return(NULL);
  139.   }
  140.   ParsePatternNoCase((UBYTE *) ending, (UBYTE *) pattern, sizeof(pattern));
  141.  
  142.  
  143.   /*
  144.       Get default file- and drawername.
  145.   */
  146.   if (defaultfile  &&  *defaultfile != '\0')
  147.   { strcpy(FileRequestName, defaultfile);
  148.   }
  149.   else
  150.   { if (TrnFileName != '\0')
  151.     { strcpy(FileRequestName, TrnFileName);
  152.     }
  153.     else
  154.     { sprintf(FileRequestName, savemode ? "chaos.%d.cdat" : "", NumRounds);
  155.     }
  156.   }
  157.   filename = (char *) FilePart((STRPTR) FileRequestName);
  158.   strcpy(PathName, FileRequestName);
  159.   *(pathname = (char *) PathPart((STRPTR) PathName)) = '\0';
  160.  
  161.  
  162.   /*
  163.       Make the drawername absolute.
  164.   */
  165.   dir = Lock((STRPTR) PathName, SHARED_LOCK);
  166.   NameFromLock(dir, (STRPTR) PathName, sizeof(PathName));
  167.   UnLock(dir);
  168.  
  169.   /*
  170.      Ensure, that the default filename has the right ending.
  171.   */
  172.   if (ending != NULL  &&  (endptr = strrchr(filename, '.')) != NULL)
  173.   { strcpy(endptr, ending+2);
  174.   }
  175.  
  176.  
  177.   /*
  178.       Bring up the requester
  179.   */
  180. #ifdef V39_INCLUDES
  181.   if (MUI_AslRequestTags(requester,
  182.              ASLFR_Window, window,
  183.              ASLFR_PrivateIDCMP, TRUE,
  184.              ASLFR_SleepWindow, TRUE,
  185.              ASLFR_TitleText, title,
  186.              ASLFR_InitialFile, filename,
  187.              ASLFR_InitialDrawer, PathName,
  188.              ASLFR_InitialPattern, ending,
  189.              ASLFR_DoSaveMode, savemode,
  190.              ASLFR_RejectIcons, TRUE,
  191.              ASLFR_AcceptPattern, pattern,
  192.              TAG_DONE)  !=    FALSE    &&
  193.       requester->fr_File != NULL  &&  requester->fr_File != '\0')
  194.   { strcpy(FileRequestName, (char *) requester->fr_Drawer);
  195.     AddPart((STRPTR) FileRequestName, requester->fr_File,
  196.         sizeof(FileRequestName));
  197.     result = FileRequestName;
  198.   }
  199. #else
  200.   if (MUI_AslRequestTags(requester,
  201.              ASL_Window, window,
  202.              ASL_Hail, title
  203.              ASL_File, filename,
  204.              ASL_Dir, PathName,
  205.              TAG_DONE)  ==    FALSE    &&
  206.       requester->rf_File != NULL  &&  requester->rf_File != '\0')
  207.   { strcpy(FileRequestName, (char *) requester->rf_Dir);
  208.     AddPart((STRPTR) FileRequestName, (STRPTR) requester->rf_File,
  209.         sizeof(FileRequestName));
  210.     result = FileRequestName;
  211.   }
  212. #endif
  213.   MUI_FreeAslRequest((APTR) requester);
  214.   return (result);
  215. }
  216. #endif    /*  AMIGA   */
  217.  
  218.  
  219.  
  220.  
  221. /*
  222.     CreateIcon() puts an icon to a recently saved file.
  223.  
  224.     Inputs: name of the file just created; must not be NULL
  225. */
  226. void CreateIcon(char *name)
  227.  
  228. #ifdef AMIGA
  229. { extern int MakeIcons;
  230.  
  231.   /*
  232.       Does the user want to have an icon?
  233.   */
  234.   if (MakeIcons)
  235.   { /*
  236.     Yes, get a diskobject
  237.     */
  238.     struct DiskObject *dobj;
  239.     char *olddeftool;
  240.     int len = strlen(IconName);
  241.  
  242.     /*
  243.     Icon.library doesn't like a trailing ".info" when calling
  244.     GetDiskObject().
  245.     */
  246.     if (len >= 5  &&
  247.     Stricmp((STRPTR) IconName+len-5, (STRPTR) ".info") == 0)
  248.     { IconName[len-5] = '\0';
  249.     }
  250.  
  251.     if ((dobj = GetDiskObject((STRPTR) IconName))  !=  NULL  ||
  252.     (dobj = GetDiskObject((STRPTR) "s:Chaos_Project"))  !=  NULL  ||
  253.     (dobj = GetDefDiskObject(WBPROJECT))  !=  NULL)
  254.     { /*
  255.       Put the right settings into the diskobject and save it.
  256.       */
  257.       dobj->do_Type = WBPROJECT;
  258.       olddeftool = dobj->do_DefaultTool;
  259.       dobj->do_DefaultTool = ProgName;
  260.       dobj->do_CurrentX = dobj->do_CurrentY = NO_ICON_POSITION;
  261.       if (dobj->do_StackSize < 20000)
  262.       { dobj->do_StackSize = 20000;
  263.       }
  264.       PutDiskObject((STRPTR) name, dobj);
  265.       dobj->do_DefaultTool = olddeftool;
  266.       FreeDiskObject(dobj);
  267.     }
  268.   }
  269. }
  270. #else    /*  !AMIGA  */
  271. {   /*
  272.     There is nothing to be done on other systems.
  273.     */
  274. }
  275. #endif    /*  !AMIGA  */
  276.  
  277.  
  278.  
  279.  
  280. /*
  281.     AskSave brings up a requester asking the user, if he wants to save
  282.     first.
  283. */
  284. int AskSave(void)
  285.  
  286. #ifdef AMIGA
  287. {
  288.   switch (MUI_RequestA(App, MainWnd, 0,
  289.                (char *) GetChaosString(MSG_ATTENTION),
  290.                (char *) GetChaosString(MSG_YES_NO_CANCEL),
  291.                (char *) GetChaosString(MSG_CHANGES_MADE), NULL))
  292.   { case 2:
  293.       return(TRUE);
  294.     case 1:
  295.       return(SaveTournament(NULL));
  296.   }
  297.   return(FALSE);
  298. }
  299. #endif    /*  AMIGA   */
  300.  
  301.  
  302.  
  303.  
  304. /*
  305.     The TerminateTrnWnd() function closes the tournament input window.
  306. */
  307. #ifdef AMIGA
  308. static APTR TrnWnd = NULL; /*  Tournament window           */
  309. static APTR TrnOkGad;       /*  Ok gadget (tournament window)       */
  310. static APTR TrnCancelGad;  /*  Cancel gadget (tournament window)   */
  311. static APTR TrnNameGad;    /*  Tournament name gadget           */
  312. static APTR WinnerPointsGad; /* Winner points gadget           */
  313. static APTR DrawPointsGad;   /* Draw points gadget           */
  314.  
  315. void TerminateTrnWnd(void)
  316.  
  317. { if (TrnWnd)
  318.   { set(TrnWnd, MUIA_Window_Open, FALSE);
  319.     DoMethod(App, OM_REMMEMBER, TrnWnd);
  320.     MUI_DisposeObject(TrnWnd);
  321.     TrnWnd = NULL;
  322.   }
  323. }
  324. #endif    /*  AMIGA   */
  325.  
  326.  
  327.  
  328.  
  329. /*
  330.     The InitTrnWnd() function brings up a window, that allows to input
  331.     tournament data.
  332.  
  333.     Inputs: name    pointer to a buffer, that can hold the tournament name
  334.         winnerpoints    current number of points for winning a game
  335.         drawpoints        current number of points for a draw
  336.  
  337.     Results: TRUE, if successfull, FALSE otherwise
  338. */
  339. #ifdef AMIGA
  340. #define ID_TrnWnd_Cancel    201
  341. #define ID_TrnWnd_Ok        202
  342. int InitTrnWnd(char *buffer, int winnerpoints, int drawpoints)
  343.  
  344. { ULONG open;
  345.   int OK_SC = *GetChaosString(MSG_OK_SC);
  346.   int Cancel_SC = *GetChaosString(MSG_CANCEL_SC);
  347.  
  348.   /*
  349.       Open the window and check for success.
  350.   */
  351.   TrnWnd = WindowObject,
  352.         MUIA_Window_ID, MAKE_ID('T','R','N','I'),
  353.         MUIA_Window_Title, GetChaosString(MSG_TOURNAMENT_INPUT_TITLE),
  354.         MUIA_Window_Width, MUIV_Window_Width_MinMax(40),
  355.         WindowContents, VGroup,
  356.         Child, HGroup,
  357.             Child, Label2(GetChaosString(MSG_TOURNAMENT_NAME_OUTPUT)),
  358.             Child, TrnNameGad = StringObject,
  359.             StringFrame,
  360.             MUIA_String_MaxLen, TRNNAME_LEN+1,
  361.             MUIA_String_Contents, buffer,
  362.             End,
  363.         End,
  364.         Child, VSpace(0),
  365.         Child, HGroup,
  366.             Child, VGroup,
  367.             Child, Label2(GetChaosString(MSG_WINNERPOINTS)),
  368.             Child, Label2(GetChaosString(MSG_DRAWPOINTS)),
  369.             End,
  370.             Child, VGroup,
  371.             Child, WinnerPointsGad = StringObject,
  372.                 StringFrame,
  373.                 MUIA_String_MaxLen, 3,
  374.                 MUIA_String_Accept, "0123456789 ",
  375.                 MUIA_String_Integer, winnerpoints,
  376.             End,
  377.             Child, DrawPointsGad = StringObject,
  378.                 StringFrame,
  379.                 MUIA_String_MaxLen, 3,
  380.                 MUIA_String_Accept, "0123456789 ",
  381.                 MUIA_String_Integer, drawpoints,
  382.             End,
  383.             End,
  384.             Child, HSpace(0),
  385.         End,
  386.         Child, VSpace(0),
  387.         Child, HGroup,
  388.             MUIA_Group_SameSize, TRUE,
  389.             Child, TrnOkGad = KeyButton(GetChaosString(MSG_OK), OK_SC),
  390.             Child, HSpace(0),
  391.             Child, TrnCancelGad = KeyButton(GetChaosString(MSG_CANCEL_INPUT), Cancel_SC),
  392.         End,
  393.         End,
  394.     End;
  395.   if (!TrnWnd)
  396.   { return(FALSE);
  397.   }
  398.   DoMethod(App, OM_ADDMEMBER, TrnWnd);
  399.   DoMethod(TrnWnd, MUIM_Window_SetCycleChain, TrnNameGad, TrnOkGad,
  400.        TrnCancelGad, NULL);
  401.   set(TrnWnd, MUIA_Window_ActiveObject, TrnNameGad);
  402.  
  403.  
  404.   /*
  405.       Setting up the notification events for the tournament input window:
  406.       CloseWindow, Ok- and Cancel Gadget
  407.   */
  408.   DoMethod(TrnWnd, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, App, 2,
  409.        MUIM_Application_ReturnID, ID_TrnWnd_Cancel);
  410.   DoMethod(TrnWnd, MUIM_Notify, MUIA_Window_InputEvent, "ctrl return",
  411.        App, 2, MUIM_Application_ReturnID, ID_TrnWnd_Ok);
  412.   DoMethod(TrnOkGad, MUIM_Notify, MUIA_Pressed, FALSE, App, 2,
  413.        MUIM_Application_ReturnID, ID_TrnWnd_Ok);
  414.   DoMethod(TrnCancelGad, MUIM_Notify, MUIA_Pressed, FALSE, App, 2,
  415.        MUIM_Application_ReturnID, ID_TrnWnd_Cancel);
  416.   DoMethod(TrnWnd, MUIM_Window_SetCycleChain,
  417.        TrnNameGad, WinnerPointsGad, DrawPointsGad, TrnOkGad, TrnCancelGad,
  418.        NULL);
  419.   DoMethod(TrnNameGad, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
  420.        TrnWnd, 3, MUIM_Set, MUIA_Window_ActiveObject, WinnerPointsGad);
  421.   DoMethod(WinnerPointsGad, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
  422.        TrnWnd, 3, MUIM_Set, MUIA_Window_ActiveObject, DrawPointsGad);
  423.   DoMethod(DrawPointsGad, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
  424.        TrnWnd, 3, MUIM_Set, MUIA_Window_ActiveObject, TrnNameGad);
  425.  
  426.  
  427.   set(TrnWnd, MUIA_Window_Open, TRUE);
  428.   get(TrnWnd, MUIA_Window_Open, &open);
  429.   if (!open)
  430.   { MUIError((char *) GetChaosString(ERRMSG_CANNOT_OPEN_WINDOW));
  431.     TerminateTrnWnd();
  432.     return(FALSE);
  433.   }
  434.   set(TrnWnd, MUIA_Window_ActiveObject, TrnNameGad);
  435.  
  436.   set(MainWnd, MUIA_Window_Open, FALSE);
  437.   return(TRUE);
  438. }
  439. #endif    /*  AMIGA   */
  440.  
  441.  
  442.  
  443.  
  444. /*
  445.     The ProcessTrnWnd() function waits for user actions concerning
  446.     the tournament input window.
  447.  
  448.     Inputs: buffer  pointer to a string, that holds the users input.
  449.             (Not needed on the Amiga.)
  450.         winnerpoints    pointer to an int where to store the
  451.                 number of points for winning a game
  452.                 game
  453.         drawpoints        pointer to an int where to store the
  454.                 number of points for a draw
  455.  
  456.     Results:    0   Indicates, that the user has cancelled.
  457.         1   Indicates, that this has to be called again.
  458.         -1  Terminating via Ok-Gadget, okay
  459. */
  460. int ProcessTrnWnd(char *buffer, int *winnerpoints, int *drawpoints)
  461.  
  462. #ifdef AMIGA
  463. { ULONG Signal;
  464.   char *name;
  465.  
  466.   /*
  467.       Check for user actions
  468.   */
  469.   switch (DoMethod(App, MUIM_Application_Input, &Signal))
  470.   { case MUIV_Application_ReturnID_Quit:
  471.       if (TestSaved())
  472.       { exit(0);
  473.       }
  474.       break;
  475.     case ID_TrnWnd_Cancel:
  476.       return(0);
  477.     case ID_TrnWnd_Ok:
  478.       /*
  479.       Get the final state of the tournament name gadget.
  480.       */
  481.       if (buffer)
  482.       { get(TrnNameGad, MUIA_String_Contents, &name);
  483.     strcpy (buffer, name);
  484.     get(WinnerPointsGad, MUIA_String_Integer, winnerpoints);
  485.     get(DrawPointsGad, MUIA_String_Integer, drawpoints);
  486.       }
  487.       return(-1);
  488.   }
  489.   if (Signal)
  490.   { Wait(Signal);
  491.   }
  492.   return(1);
  493. }
  494. #endif
  495.